<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
	<title>富文本编辑器</title>
	<link rel="stylesheet" href="/static/component/layui/css/layui.css" />
	<link rel="stylesheet" href="/static/admin/css/pearForm.css" />
	<link href="/static/admin/css/pearSelect.css" rel="stylesheet" />

	<style>
		body{margin: 10px;}
	</style>
</head>
<body style="background-color:#ffffff!important">
	<div class="layui-row">
		<form class="layui-form" style="margin-left: 20px;margin-right: 20px" action="" lay-filter="component-form-element">

			<div class="layui-form-item">
				<input type="text" name="title" lay-verify="required" placeholder="标题" autocomplete="off" class="layui-input">
			</div>

			<div class="layui-form-item">
				<div class="layui-input-inline">
					<input type="text" id="startAt" name="start_at" lay-verify="required" placeholder="生效时间" autocomplete="off" class="layui-input">
				</div>
				<div class="layui-form-mid">-</div>
				<div class="layui-input-inline">
					<input type="text" id="endAt" name="end_at" lay-verify="required" placeholder="结束时间" autocomplete="off" class="layui-input">
				</div>
			</div>

			<div class="layui-form-item">
				<textarea name="content" id="content" placeholder="描述" class="layui-textarea"></textarea>
			</div>

			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
				</div>
			</div>
		</form>
	</div>

<script src="/static/component/layui/layui.js"></script>
<script src="/static/blog/jquery-3.1.1.min.js"></script>

<script>

	layui.use(['util', 'layer','form', "laydate", "layedit"], function () {
		let util = layui.util;
		let form = layui.form;
		let laydate = layui.laydate;
		let layedit = layui.layedit;

		laydate.render({
			elem: '#startAt',
			type: 'datetime'
		});
		laydate.render({
			elem: '#endAt',
			type: 'datetime'
		});

		let content = layedit.build('content'); //建立编辑器

		form.on('submit(submit)', function(data){
			data.field["content"] = layedit.getContent(content);
			$.ajax({
				url: '/api/admin/notice/create',
				type: 'post',
				data: JSON.stringify(data.field),
				dataType: "json",
				contentType: 'application/json',
				success: function (response) {
					if (response.code === 1) {
						layer.alert(response.msg);
					} else {
						let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
						parent.location.reload();//刷新父页面，注意一定要在关闭当前iframe层之前执行刷新
						parent.layer.close(index); //再执行关闭
					}
				}
			});
			return false;
		});

	});
</script>
</body>
</html>